package fi.profecons.csswindows;

import java.util.Vector;

import fi.profecons.csswindows.toolkit.ContentPane;
import fi.profecons.csswindows.toolkit.FileUpload;
import fi.profecons.csswindows.toolkit.Frame;

/**
 * This a base class for all containers. Containers can contain elements and other containers. 
 * Containers should be used for organizing the user interface elements.
 * @author ProfeCons Oy - Semi Arajuuri
 *
 */
public class Container extends RestrictedContainer {

	public Container() {
		super();
	}
	
	/** 
	 * Add a element into this container. The element can also be another container.
	 * @param element All user interface elements inherit from Element class. 
	 */
	public void add(Element element) {
		getAll().add(element);
		element.setState(getState());
		addCommand("getElement('" + getId() + "').addElement(getElement('"
				+ element.getId() + "'));");
		if (element instanceof Frame) {
			addCommand("getElement('" + element.getId()
					+ "').setParent(getElement('" + getId() + "'))");
		}
		element.setParent(this);
	}
	
	protected void init(ContentPane contentPane){
		getAll().add(contentPane);
		contentPane.setState(getState());
	}
	
	/**
	 * Remove a element from this container.
	 * 
	 * @param element Remove this element.
	 * @param cascade Should also the elements that are linked to this one be removed? 
	 * 				  Linked elements are elements that are not directly under this element in
	 *				  browsers DOM, but are listed as such on the server side. Most of the time
	 *				  you should not need to explicitly remove linked elements.
	 */
	public void remove(Element element, boolean cascade) {
		getAll().remove(element);
		if (cascade) {
			addCommand("getElement('" + element.getId() + "').remove(true);");
		} else {
			addCommand("getElement('" + element.getId() + "').remove(false);");
		}
	};

	/**
	 * Remove a element from this container, but do not remove linked elements.
	 * 
	 * @param element Remove this element.
	 */
	public void remove(Element element) {
		remove(element, false);
	}
	
	public Vector getAll() {
		return super.getAll();
	}

	protected void setFileUpload(FileUpload fileUpload){
		getState().setFileUpload(fileUpload);
	}
}
